package com.acme.bankapp.domain.account;

import com.acme.bankapp.domain.exception.NotEnoughFundsException;
import com.acme.bankapp.domain.exception.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount{
	private double overdraft = 0;
	
	public CheckingAccount(double balance, double overdraft){
		super(balance);
		if(overdraft < 0)
			throw new IllegalArgumentException();
		this.overdraft = overdraft;
	}
	
	public CheckingAccount(double balance){
		super(balance);
	}

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}
	
	public double maximumAmountToWithdraw() {
		return getBalance() + getOverdraft();
	}
	
	
	public void withdraw(double x) throws NotEnoughFundsException{
		if(x >= 0){
			if(x >= super.getBalance()){
				withdrawOverdraft(x);
			} else {
				super.setBalance(super.getBalance() - x);
			}
		}		
	}
	
	private void withdrawOverdraft(double x) throws OverDraftLimitExceededException{
		double curbalance = super.getBalance();
		if(overdraft + curbalance >= x){
			super.setBalance(curbalance - x);
		} else {
			String tmpmsg = "Not enough money for withdraw, max withdraw for client = " + (super.getBalance() + overdraft);
			throw new OverDraftLimitExceededException(tmpmsg, (super.getBalance() + overdraft));
		}
	}
	
	public boolean isValidBalance(){
		boolean checksuccess = true;
		if(super.getBalance() + this.overdraft < 0){
			checksuccess = false;
		}
		return checksuccess;
	}
	
}
